<?php

class Controller_Sitemap extends Controller_Base
{
	private $c_array = array();
	
	public function __construct($parts) {		
		$this->parts        = $parts;
		$this->common_vars  = Registry::get("common_vars");
		$this->table_module = PRFX."structure";
	}
	
	private function getStructure($parent = 1) 
	{		
		$r =& Registry::get("MDB")->query("SELECT 
		  ".DB_STRUCTURE.".ID AS structure_id,
		  ".DB_STRUCTURE.".Name AS structure_name,
		  ".DB_STRUCTURE.".Sys_name AS structure_sys_name,
		  ".DB_STRUCTURE.".Parent AS structure_parent_id,
		  ".DB_STRUCTURE.".Redirect as structure_redirect,
		  ".PRFX."modules.Controller AS controller_file,
		  ".PRFX."modules.Db_name as controller_db_name  
		FROM
		  ".DB_STRUCTURE."
		  INNER JOIN ".PRFX."modules ON (".DB_STRUCTURE.".Module = ".PRFX."modules.ID)
		  INNER JOIN ".PRFX."templates ON (".DB_STRUCTURE.".Template = ".PRFX."templates.ID)
		WHERE
		  ".DB_STRUCTURE.".Parent = '".$parent."' AND 
		  ".DB_STRUCTURE.".Show = '1' 
		ORDER BY 
		  ".DB_STRUCTURE.".Por;");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}				
		if($r->numRows()) {					
			while($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				$this->c_array[$p["structure_parent_id"]][] = $p;
				self::getStructure($p["structure_id"]);
			}			
		}
	}
	
	private function getCars() 
	{
		$r =& Registry::get("MDB")->query("SELECT 
			`Sys_name`,
			`Name`,
			`Redirect` 
		FROM 
			`".PRFX."cars` 
		WHERE 
			`Parent`='0' AND 
			`Show`='1' 
		ORDER BY 
			`Por`;");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}				
		if($r->numRows()) {	
			while($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				$c_array[] = $p;
			}
			return $c_array;
		}
	}
	
	private function getNews($section = array()) {
		if(!empty($section)) {
			foreach($section as $id => $sys_name) {
				$r =& Registry::get("MDB")->query("SELECT 
					`Sys_name`,
					`Name`
				FROM 
					`".PRFX."news` 
				WHERE 
					`Parent`='".$id."' AND 
					`Show`='1' 
				ORDER BY 
					`Date`
				DESC;");
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}				
				if($r->numRows()) {	
					while($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
						$out[$sys_name][] = $p;
					}
				}
			}
			return $out;
		}
	}

    public function out()
	{
		self::getStructure();
		
		$out['header'] = $this->common_vars["structure_name"];		
		$out['item_structure'] = $this->c_array;
		$out['item_cars'] = self::getCars();
		$out['item_news'] = self::getNews(array(
			"56" => "news"
		));
		
		/*echo "<pre>";
		print_r($out);
		echo "</pre>";*/
		
		Registry::set("title", functions::createTitle(array($this->common_vars["structure_name"]), ''));	
		
		Registry::get("smarty")->assign($out);
		return Registry::get("smarty")->fetch(TEMPLATE_SITE.$this->common_vars["controller_template"], Registry::get("cache_id"));	
    }
}

$controller = new Controller_Sitemap($parts);
$site_module_out = $controller->out();

?>